package com.ibeeking.found.file.rest.config.onlyoffice;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ibeeking.found.file.rest.service.office.documentserver.storage.FileStoragePathBuilder;
import com.ibeeking.found.file.rest.service.office.documentserver.util.SSLUtils;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class OnlyofficeConfiguration {

    @Value("${files.storage.address}")
    private String storageAddress;

    @Value("${files.docservice.verify-peer-off}")
    private boolean verifyPerrOff;

    @Autowired
    private FileStoragePathBuilder storagePathBuilder;

    @Autowired
    private SSLUtils ssl;

    @Bean
    public ModelMapper mapper(){  // create the model mapper
        ModelMapper mapper = new ModelMapper();
        mapper.getConfiguration()  // get the mapper configuration and set new parameters to it
                .setMatchingStrategy(MatchingStrategies.STRICT)  // specify the STRICT matching strategy
                .setFieldMatchingEnabled(true)  // define if the field matching is enabled or not
                .setSkipNullEnabled(true)  // define if null value will be skipped or not
                .setFieldAccessLevel(org.modelmapper.config.Configuration.AccessLevel.PRIVATE);  // specify the PRIVATE field access level
        return mapper;
    }

//    @Bean
//    public JSONParser jsonParser(){  // create JSON parser
//        return new JSONParser();
//    }

    @PostConstruct
    public void init(){  // initialize the storage path builder
        storagePathBuilder.configure(StringUtils.isEmpty(storageAddress) ? null : storageAddress);

        try{
            if(verifyPerrOff) {
                ssl.turnOffSslChecking(); //the certificate will be ignored
            } else {
                ssl.turnOnSslChecking(); //the certificate will be verified
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    @Bean
    public ObjectMapper objectMapper(){  // create the object mapper
        return new ObjectMapper();
    }
}
